#pragma once 

#include <iostream>
#include <string>
#include <functional>
#include "InetAddr.hpp"


class Reactor;
using func_t = std::function<void(Reactor*)>;

//这里我们对于listen和读写两种fd的处理都使用同一个类型，根据用户注册进来的方法调用不同的处理方式。
class Connection 
{
public:
    Connection(int sockfd)
        :_sockfd(sockfd)
        {
        }
    void InitCB(func_t reader,func_t writer,func_t excepter)
    {
        _reader = reader; 
        _writer = writer;
        _excepter = excepter;
    }
    void CallRead(Reactor*owner) { _reader(owner);}
    void CallWrite(Reactor*owner) { _writer(owner);}
    
    void SetPeerInfo(const InetAddr& peer) { _peer = peer;}
    void Setevents(uint32_t events) { _events = events;}
    uint32_t Getevents() {return _events;}
    int Fd() { return _sockfd;}
    ~Connection(){}
private:
    int _sockfd;
    std::string rbuffer;
    std::string wbuffer;
    uint32_t _events;
    InetAddr _peer;
 
    Reactor* _self; //指向自己属于哪一个epoll服务
    func_t _reader;
    func_t _writer;
    func_t _excepter;

};